#!/usr/bin/env bash

function install_docker_dependecs(){
    case "$ID" in
      ubuntu*)
        install_software apt-transport-https
        install_software ca-certificates
        install_software curl
        install_software software-properties-common
        ;;
      rhel*|centos*)
        install_software curl
        install_software wget
        ;;
    esac

}

function configure_docker_daemon() {
    echo configuring /etc/docker/daemon.json
    echo /etc/docker/daemon.json content is
    cat ${SCRIPT_DIR}/setClusterCommon/files/docker_daemon_json

    mkdir -p /etc/docker
    cp ${SCRIPT_DIR}/setClusterCommon/files/docker_daemon_json  /etc/docker/daemon.json
    chmod 0644 /etc/docker/daemon.json

    echo finish configuring /etc/docker/daemon.json
}

function configure_docker_apt() {
    echo bengin to configure_docker_apt
    case "$ID" in
      ubuntu*)
        curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
        add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
        apt-get update --fix-missing
        ;;
      rhel*|centos*)
        wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
        ;;
    esac

    echo finish configure_docker_apt
}

function install_nv_docker() {
    if ! lspci | grep NVIDIA >/dev/null 2>&1; then
        echo [install_nv_docker] node has no NVIDIA GPU
        return 0
    fi
    distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
    case "$ID" in
      ubuntu*)
        curl -s -L https://nvidia.github.io/nvidia-container-runtime/gpgkey |  apt-key add -
        curl -s -L https://nvidia.github.io/nvidia-container-runtime/$distribution/nvidia-container-runtime.list | \
          tee /etc/apt/sources.list.d/nvidia-container-runtime.list
        apt-get update
        apt-get install -y --allow-downgrades nvidia-container-runtime
        ;;
      rhel*|centos*)
        curl -s -L https://nvidia.github.io/nvidia-container-runtime/$distribution/nvidia-container-runtime.repo | \
          tee /etc/yum.repos.d/nvidia-container-runtime.repo
        yum -y install nvidia-container-runtime
        ;;
    esac

    cp ${SCRIPT_DIR}/setClusterCommon/files/nv_docker_daemon_json  /etc/docker/daemon.json
    chmod 0644 /etc/docker/daemon.json

    systemctl restart docker
}

function login_private_hub(){
    login_shell_file="${SCRIPT_DIR}/docker_login.sh"
    if test -f ${login_shell_file}; then
        bash ${login_shell_file}
    fi
}

function install_docker() {
    install_docker_dependecs
    configure_docker_apt
    configure_docker_daemon

    case "$ID" in
      ubuntu*)
        docker_version=${DOCKER_CE_VESION}
        apt-get install -y --allow-downgrades ${docker_version}
        echo installed docker-ce=${docker_version}
        docker info
        ;;
      rhel*|centos*)
        docker_version=${DOCKER_CE_VESION}
        yum install -y ${docker_version}
        echo installed docker-ce=${docker_version}
        docker info
        ;;
    esac

    install_nv_docker

    login_private_hub
}